package com.songcha.module_bookdetail.ui.activity.more_comment

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreViewModel
import com.songcha.library_business.bean.book.BookDetailNetBean
import com.songcha.library_business.proxy.database.BookShelfDaoManager
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_bookdetail.bean.BookCommentListBean

class MoreBookCommentViewModel(app: Application):BaseRefreshLoadMoreViewModel<MoreBookCommentRepository,BookCommentListBean.DataBean.CommentBean>(app) {
    var bookDetail:BookDetailNetBean.DataBean?=null
    //val bookCommentList=MutableLiveData<BookCommentListDataBean.BookCommentListBean>()
    val bookCommentTotalCount=MutableLiveData<Int>()
    var isInBookShelf=MutableLiveData(false)

    fun addBookToShelf(){
        if(BookShelfDaoManager.addToBookShelf(bookDetail!!.bookId.toString())){
            isInBookShelf.value=true
        }
        if(UserInfoManager.checkLogin()){
            handleApiDataObserver(repository!!.addToBookShelf(bookDetail!!.bookId),object :
                IApiResult<BaseBean> {
                override fun onSuccess(bean: BaseBean) {

                }
            })
        }
    }

    fun removeBookFromShelf(){
        BookShelfDaoManager.removeFromBookShelf(bookDetail!!.bookId.toString())
        isInBookShelf.value=false
    }

    fun getIsBookInShelf(){
        isInBookShelf.value=BookShelfDaoManager.isInBookShelf(bookDetail!!.bookId.toString())
    }

    fun getBookComment(){
        handleApiDataObserver(repository!!.getBookCommentList(bookDetail!!.bookId.toString(),currentPage,10),object :
            IApiResult<BookCommentListBean> {
            override fun onSuccess(bean: BookCommentListBean) {
               // bookCommentList.value=bean.data
                bookCommentTotalCount.value = bean.data.total
                handleDataListSuccess(bean.data.records)
            }

            override fun onError(e: Throwable) {

            }
        },true)

    }



}